﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace LoveNote.Database.Helpers
{
    public class ImageHelper
    {

        public static string InteropBitmapToBase64String(InteropBitmap bitmap)
        {
            // 创建一个IntPtr，用于存储位图数据
            int height = (int)bitmap.Height;
            int width = (int)bitmap.Width;
            int stride = width * ((bitmap.Format.BitsPerPixel + 7) / 8);
            int bufferSize = stride * height;
            IntPtr ptr = Marshal.AllocHGlobal(bufferSize);

            // 锁定位图数据，并将数据复制到IntPtr
            bitmap.CopyPixels(Int32Rect.Empty, ptr, bufferSize, stride);

            // 将IntPtr转换为byte数组
            byte[] pixels = new byte[bufferSize];
            Marshal.Copy(ptr, pixels, 0, bufferSize);
            Marshal.FreeHGlobal(ptr);

            // 使用Convert.ToBase64String转换为Base64字符串
            return Convert.ToBase64String(pixels);
        }


        /// <summary>
        /// 获取图片的二进制流
        /// </summary>
        /// <param name="fileName">文件名</param>
        public static string GetImageString(string fileName)
        {
            try
            {
                FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);
                return Convert.ToBase64String(buffer);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return string.Empty;
            }
        }

        /// <summary>
        /// 二进制流转换成图片源
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static BitmapImage GetBitmapImage(string _buffer)
        {
            if (string.IsNullOrEmpty(_buffer))
            {
                return new BitmapImage();
            }

            byte[] buffer = Convert.FromBase64String(_buffer);
            MemoryStream memoryStream = new MemoryStream(buffer, 0, buffer.Length);
            memoryStream.Write(buffer, 0, buffer.Length);
            BitmapImage image = new BitmapImage();
            memoryStream.Position = 0;
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.StreamSource = memoryStream;
            image.EndInit();
            image.Freeze();
            return image;
        }

        /// <summary>
        /// 把内存里的BitmapImage数据保存到硬盘中
        /// </summary>
        /// <param name="bitmapImage">BitmapImage数据</param>
        /// <param name="filePath">输出的文件路径</param>
        public static void SaveBitmapImageIntoFile(BitmapImage bitmapImage, string filePath)
        {
            BitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bitmapImage));

            using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            {
                encoder.Save(fileStream);
            }
        }

    }
}
